var totalPage;
var totalCnt;
var subBranchCompanyData = [];
var branchCompanyData = [];
var lastheaderNameEN = undefined;
var deleteFlag;

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	createTableHead("subBranchCompanyThead", subBranchCompanyTable, search);
	scrollTableSuspend("scrollBody", "subBranchCompanyTable", "subBranchCompanyTable1", "subBranchCompanyThead", "scrollHeight");
	$ajaxList('branchCompany', getBranchCompany);
	search();
	setTimeout(hideLoading, 500);
})
function getBranchCompany(data) {
	var length = data.length;
	for (var i = 0;i < length; i++){
    	branchCompanyData = data;
        $("#BranchCompanyID").append("<option value='" + data[i].branchCompanyID + "'>" + data[i].branchCompanyName + "</option>");
    }
}
function resetSearch(){
	setTimeout(search, 500);
}
function search(headerNameEN) {
	if(lastheaderNameEN != headerNameEN){
		clearOrder(lastheaderNameEN);
		lastheaderNameEN = headerNameEN;
	}
	$ajax('searchSubBranchCompany', 'subBranchCompanySearch', searchSubBranchCompany);
}
function searchSubBranchCompany(data) {
	totalPage = data && data.totalPage || 0;
	totalCnt = data && data.totalCnt || 0;
	subBranchCompanyData = data && data.subBranchCompanyList || [];
	createTableBody("subBranchCompanyTbody", subBranchCompanyTable, subBranchCompanyData);
	setTimeout(hideLoading, 500);
}
function chkBefore(index, chkFlag){
	var checkbox =document.getElementsByName("chk"); 
	for (var i=0; i<checkbox.length; i++) {
		checkbox[i].checked = false; 
	}
	checkbox[index].checked = true; 
	if (chkFlag == "update") {
		update(index);
	} else if(chkFlag == "delete") {
		del();
	}
}
function save() {
	deleteFlag = "save";
	addTitle("subBranchCompanySave");
	allTable();
}
function update(index) {
	$('#title').html('');
	deleteFlag = "update";
	addTitle("subBranchCompanyUpdate");
	allTable(index);
}
function allTable(index){
	createDetailHead("detailThead", subBranchCompanyTable, 1);
	createDetailBody("detailTbody", subBranchCompanyTable, subBranchCompanyData[index], 1);
	for (var i = 0;i < branchCompanyData.length;i++){
        $("#branchCompanyID").append("<option value='" + branchCompanyData[i].branchCompanyID + "'>" + branchCompanyData[i].branchCompanyName + "</option>");  
	}
	if(deleteFlag == "update") {
		$("#subBranchCompanyID").attr("readonly", true);
		$("#branchCompanyID").val(subBranchCompanyData[index].branchCompanyID);
		if(subBranchCompanyData[index].enabled == true){
			$("#enabled").val(1);
		}else{
			$("#enabled").val(0);
		}
	} else if(deleteFlag == "save") {
		$("#subBranchCompanyID").attr("readonly", false);
		$('#confirm').show();
	}
	$("#save").modal("show");
}
function confirm(){
	return checkForm("saveForm", subBranchCompanyTable);
}
function getUrl(){
	if(deleteFlag == "save"){
		return 'saveSubBranchCompany';
	} else if(deleteFlag == "update"){
		return 'updateSubBranchCompany';
	} else if(deleteFlag == "delete"){
		return 'deleteSubBranchCompany';
	}
}
function getData(){
	return {subBranchCompanyIDList: getCheckDataList(subBranchCompanyData, "subBranchCompanyID")};
}
function saveSubBranchCompany(data){
	if(deleteFlag == "save"){
	     var result = data && data.result;
	     if (result == "Success") {
	    	 showMessge(messageContent["Insert.Success"]);
	    	 $('#save').modal('hide');
	    	 formReset("saveForm");
	     } else if (result == "NotFound"){
	    	 showMessge(messageContent["Insert.Failed"]);
	     } else if (result == "SubBranchCompanyIDExist"){
	         showMessge(messageContent["SubBranchCompany.SubBranchCompanyIDExist"]);
	     } else if (result == "SubBranchCompanyNameExist"){
		         showMessge(messageContent["SubBranchCompany.SubBranchCompanyNameExist"]);
	     } else {
	    	 showMessge(messageContent["Insert.Error"]);
	     }
	     search();
	}else if(deleteFlag == "update"){
	     var result = data && data.result;
	     if (result == "Success"){
	    	 showMessge(messageContent["Update.Success"]);
	    	 $('#save').modal('hide');
	    	 formReset("saveForm");
	     } else if (result == "NotFound"){
	    	 showMessge(messageContent["Update.Failed"]);
	     } else if (result == "SubBranchCompanyIDExist"){
	         showMessge(messageContent["SubBranchCompany.SubBranchCompanyIDExist"]);
	     } else if (result == "SubBranchCompanyNameExist"){
		         showMessge(messageContent["SubBranchCompany.SubBranchCompanyNameExist"]);
	     } else{
	    	 showMessge(messageContent["Update.Error"]);
	     }
	     search();
	}
}
function sure(data){
    var result = data && data.result;
    if (result == "Success") {
		showMessge(messageContent["Delete.Success"]);
	} else if (result == "NotFound") {
		showMessge(messageContent["Delete.NotFound"]);
	} else {
		showMessge(messageContent["Delete.Error"]);
	}
    search();
}